//
//  CAMembersSearchHeaderView.m
//  LNH
//
//  Created by twentylx on 2018/1/26.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CAMembersSearchHeaderView.h"

@implementation CAMembersSearchHeaderView

/**
 为视图绑定 viewModel
 
 @param viewModel 要绑定的ViewModel
 @param params 额外参数
 */
- (void)bindViewModel:(id <LMViewProtocol>)viewModel withParams:(NSDictionary *)params {
    
}

/**
 初始化额外数据
 */
- (void)lm_initializeForView {
}

/**
 初始化视图
 */
- (void)lm_createViewForView {
    [self setNeedsUpdateConstraints];
    
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.searchBar];
    [self.bgView addSubview:self.titleLabel];
    
    
}

#pragma mark layout
- (void)updateConstraints {
    [super updateConstraints];
    
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [_searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(20);
        make.top.equalTo(self.bgView).offset(10);
        make.right.equalTo(self.bgView).offset(-20);
        make.height.mas_equalTo(30);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(40);
        make.top.equalTo(self.searchBar.mas_bottom).offset(10);
    }];
}

#pragma mark get & set
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}

- (UISearchBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [UISearchBar new];
        _searchBar.barTintColor = [UIColor clearColor];
        _searchBar.backgroundColor = [UIColor clearColor];
        _searchBar.tintColor = [UIColor clearColor];
        _searchBar.placeholder = @"输入您要查找的成员ID";
        _searchBar.layer.cornerRadius = 10;
        _searchBar.backgroundImage = [self GetImageWithColor:RGBCOLOR(241, 241, 241) andHeight:30];
        [_searchBar setSearchFieldBackgroundImage:[self GetImageWithColor:RGBCOLOR(241, 241, 241) andHeight:30] forState:UIControlStateNormal];
        
        UITextField *searchField = [_searchBar valueForKey:@"_searchField"];
        // 输入文本颜色
//        searchField.textColor = RGBCOLOR(207, 89, 38);
        searchField.font = [UIFont systemFontOfSize:16];
        // 默认文本颜色
//        [searchField setValue:RGBCOLOR(207, 89, 38) forKeyPath:@"_placeholderLabel.textColor"];
    }
    return _searchBar;
}
//- (UITextField *)searchTextField {
//    if (!_searchTextField) {
//        _searchTextField = [UITextField new];
//        _searchTextField.font = [UIFont systemFontOfSize:14];
//        _searchTextField.placeholder = @"输入您要查找的成员ID";
//        _searchTextField.layer.cornerRadius = 5;
//        _searchTextField.backgroundColor = RGBCOLOR(241, 241, 241);
//    }
//    return _searchTextField;
//}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.text = @"找人";
        _titleLabel.textColor = RGBCOLOR(209, 89, 38);
        _titleLabel.font = [UIFont systemFontOfSize:18];
    }
    return _titleLabel;
}

/**
 *  生成图片
 *
 *  @param color  图片颜色
 *  @param height 图片高度
 *
 *  @return 生成的图片
 */
- (UIImage*) GetImageWithColor:(UIColor*)color andHeight:(CGFloat)height
{
    CGRect r= CGRectMake(0.0f, 0.0f, 1.0f, height);
    UIGraphicsBeginImageContext(r.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, r);
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return img;
}

@end
